// BbsStateCollection.cpp: implementation of the CBbsStateCollection class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BbsStateCollection.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBbsStateCollection::CBbsStateCollection(CTelnetLayer *pTelnetLayer, CAnsiScreen *pScreen, IVirtualKeyboard *pKeyboard)
{
	m_pTelnetLayer = pTelnetLayer;
	m_pScreen = pScreen;

	m_states.Add(&m_pressEnterBbsState);
	m_states.Add(&m_pressAnyKeyBbsState);
	m_states.Add(&m_boardListBbsState);
	m_states.Add(&m_mailListBbsState);
	m_states.Add(&m_postListBbsState);
	m_states.Add(&m_announceListBbsState);
	m_states.Add(&m_readMailBbsState);
	m_states.Add(&m_readPostBbsState);
	m_states.Add(&m_mainMenuBbsState);
	m_states.Add(&m_normalMenuBbsState);
	m_states.Add(&m_unknownBbsState);
	m_states.Add(&m_nullBbsState);

	int count = m_states.GetSize();
	for(int n = 0; n < count; n++)
	{
		m_states[n]->SetKeyboard(pKeyboard);
	}

	m_activeState = &m_disconnectedBbsState;
}

CBbsStateCollection::~CBbsStateCollection()
{

}

IBbsState* CBbsStateCollection::GetActiveState()
{
	return m_activeState;
}

void CBbsStateCollection::UpdateActiveState(CPoint ptCursorCell)
{
	IBbsState *pState = &m_unknownBbsState;
	if(m_pTelnetLayer->IsConnected())
	{
		int count = m_states.GetSize();
		for(int n = 0; n < count; n++)
		{
			if(m_states[n]->CheckState(m_pScreen, ptCursorCell, m_activeState))
			{
				pState = m_states[n];
				pState->OnMouseMove(ptCursorCell, 0);
				break;
			}
		}
	}
	else
	{
		pState = &m_disconnectedBbsState;
		pState->CheckState(m_pScreen, CPoint(0, 0), m_activeState);
	}
	if(pState != m_activeState)
	{
		m_activeState->Deactivate();
		m_activeState = pState;
		m_activeState->Activate();
	}
}

void CBbsStateCollection::SetNullBbsState()
{
	if(m_activeState != &m_nullBbsState)
	{
		m_activeState->Deactivate();
		m_activeState = &m_nullBbsState;
		m_activeState->Activate();
	}
}